package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.adapter.JiluAdapter;
import com.keyinong.caizhengting.entity.JiluEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author：郑谦
 * @时间：2017-4-10 下午4:02:27
 * @类名：TuikuanActivity.java
 * @功能: 个人中心-钱包-退款记录
 */

public class TuikuanActivity extends Activity {

    HttpTools toolModel = null;
    JiluAdapter jiluAdapter;
    String token;
    TextView tv_fanhui;
    ImageButton ib_fanhui;
    private PullToRefreshListView pull_jilu;
    /**
     * 没有商品
     */
    private int offset = 0;
    private int page = 10;
    /**
     * 商品信息的集合
     */
    private ArrayList<JiluEntity> jilulist;
    private TextView tv_state;
    private RelativeLayout rl_none;
    /**
     * 上下拉刷新
     */
    private OnRefreshListener2<ListView> onrefresh = new OnRefreshListener2<ListView>() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            offset = 0;
            jilulist.clear();
            toolModel.balanceRecords(token, page + "", offset + "", "1",
                    new JsonDingdan());
            if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
                //				MyToast.mytoast(getActivity(), "当前网络不可用");
                Toast.makeText(TuikuanActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
                pull_jilu.onRefreshComplete();
                return;
            }

        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            offset += page;
            toolModel.balanceRecords(token, page + "", offset + "", "1",
                    new JsonDingdan());
            //			setSection();
            //			adapter.notifyDataSetChanged();
            if (NetWorkUtil.isNetwork(TuikuanActivity.this) == false) {
                Toast.makeText(TuikuanActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
                pull_jilu.onRefreshComplete();
                return;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_xiaofei);
        initRes();
    }

    private void initRes() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        tv_fanhui = (TextView) findViewById(R.id.tv_fanhui);
        pull_jilu = (PullToRefreshListView) findViewById(R.id.pull_jilu);
        pull_jilu.setMode(Mode.BOTH);
        pull_jilu.setOnRefreshListener(onrefresh);
        tv_fanhui.setText("退款");
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        tv_state = (TextView) findViewById(R.id.tv_state);
        rl_none = (RelativeLayout) findViewById(R.id.rl_none);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getJiluinfo();
    }

    /**
     * 获取退款记录
     */
    private void getJiluinfo() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        jilulist = new ArrayList<JiluEntity>();

        toolModel = new HttpTools(getApplicationContext());
        toolModel.balanceRecords(token, page + "", offset + "", "1",
                new JsonDingdan());
    }

    private class JsonDingdan extends JsonHttpResponseHandler {

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            super.onSuccess(statusCode, headers, response);

            try {
                Log.i("info", "消费记录----" + response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONArray data = response.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject object = data.getJSONObject(i);
                        String num = object.getString("num");
                        String date = object.getString("date");
                        jilulist.add(new JiluEntity(num, date.substring(0, 10), date.substring(10, 19)));
                    }
                    Log.e("info", "-----3");
                    if (jilulist.size() == 0) {
                        tv_state.setText("暂无退款记录");
                        rl_none.setVisibility(View.VISIBLE);
                    } else {
                        rl_none.setVisibility(View.GONE);
                    }
                    jiluAdapter = new JiluAdapter(TuikuanActivity.this, jilulist, "3");
                    pull_jilu.setAdapter(jiluAdapter);
                    pull_jilu.getRefreshableView().setSelection(offset);
                    pull_jilu.setDividerDrawable(null);
                    //				Utility.setGridViewHeightBasedOnChildren(pull_twosort);
                    pull_jilu.onRefreshComplete();
                } else {
                    Toast.makeText(TuikuanActivity.this, msg, Toast.LENGTH_SHORT).show();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}
